import java.io.File;

public class Control
{
	public void timeTest ()
	{
		int L=32, d=2;
		double H=0.0, T=1.0084;
		Ising ic = new Ising (L, d, H, T);
		long t = 10000; // milliseconds
		int c = 0;
		
		long t0 = System.currentTimeMillis ();
		while (System.currentTimeMillis () - t0 < t)
		{
			ic.update ();
			++c;
		}
		System.out.println((double) c*ic.N/t*1000+" MCS/s");
	}

	public void TcTest () // Find Tc in 0 field
	{
		int L=32, d=2;
		double H=0.0;
		Ising ic;
		
		int Ttrials=30;
		double T0=1.0, dT=0.1;

		int nav = 100;
		int nst = 100;

		double [] m_dat = new double [nst];
		double [] chi = new double [nav];
		// output is T, chi, sigma_chi
		double [][] out = new double [Ttrials][3];

		for (int Tn=0; Tn<Ttrials; ++Tn)
		{
			double T = T0 + Tn*dT;
			ic = new Ising (L, d, H, T);
			ic.randomizeSpins ();
			for (int i=0; i<nav; ++i)
			{
				for (int j=0; j<nst; ++j)
				{
					m_dat[j] = ic.magnetization ();
					ic.update ();
				}
				chi[i] = Utils.absVariance (m_dat) / T;
			}
			// Done collecting data for temperature, deal with data now
			out[Tn][0] = T; 
			out[Tn][1] = Utils.mean (chi); 
			out[Tn][2] = Utils.variance (chi) / Math.sqrt (nav);
		}
		Utils.doubleArrayOutput(out, "./data/TcTest.dat");
	}

	public void renderTest ()
	{
		int L=16, d=2;
		double H=0.55, T=1.0084;
		Ising ic = new Ising (L, d, H, T);
		int upRelax = 50;

		ic.allSpinsUp ();
		for (int i=0; i<upRelax; ++i) ic.update ();
		ic.flipH ();
		while (ic.magnetization () > 0.85) ic.update ();

		Grain gr = new Grain (ic);
		int center = gr.findCenter ();
		Draw dr = new Draw (ic, center);
	}

	public void dropletTest ()
	{
		int L=16, d=2;
		double H=0.50, T=1.0084;
		Ising ic = new Ising (L, d, H, T);
		Generate ge = new Generate (ic);
		ge.genDroplet (); // modifies ic
		Grain gr = new Grain (ic);
		int center = gr.findCenter ();
		Draw dr = new Draw (ic, center);
	}

	public void makeDrops ()
	{
		int numDrops = 500;
		int L=32, d=2;
		double H=0.5, T=1.0084;
		Ising ic = new Ising (L, d, H, T);
		Generate ge;

		for (int i=0; i<numDrops; ++i)
		{
	   		ge = new Generate (ic);
			ge.genDroplet (); 
			ic.saveIsing ("./data/drop"+i+".dat");
		}
	}

	public void profile (String dropsDirName)
	{
		String [] drops;
		Ising ic;
		Profile pr;

		File dropsDir = new File (dropsDirName);
		File [] dropsFiles = dropsDir.listFiles ();
		drops = new String [dropsFiles.length];
		for (int i=0; i<drops.length; ++i) drops[i] = dropsFiles[i].toString ();
		
		// take first drop to get distribution info
		ic = new Ising (drops[0]);
		pr = new Profile (ic, 2);
		double [] distribution = new double [pr.distribution.length];
		double [] distribution2 = new double [pr.distribution.length];
		double [] stderr = new double [pr.distribution.length];
        double [][] data = new double [pr.distribution.length][2];

		for (int i=0; i<drops.length; ++i)
		{
			ic = new Ising (drops[i]);
			pr = new Profile (ic, 4);
			pr.buildDistribution ();
			for (int j=0; j<distribution.length; ++j)
            {
				distribution[j] += pr.distribution[j]/drops.length;
				distribution2[j] += pr.distribution[j]*pr.distribution[j]/drops.length;
            }
		}
        for (int i=0; i<distribution.length; ++i)
        {
            stderr[i] = Math.sqrt ((distribution2[i] - distribution[i]*distribution[i]) / drops.length);

            data[i][0] = distribution[i];
            data[i][1] = stderr[i];
        }
		Utils.doubleArrayOutput (data, "./data/profile.dat");
	}

	public static void main (String [] args)
	{
		Control c = new Control ();
		c.profile ("/home/jellowitz/data/5d/near");
	}
}
